import Vue from 'vue';
import Vuex , { Store } from 'vuex'
import axios from 'axios'
import { Message } from 'element-ui'
Vue.use(Vuex);

export default new Store({
    state: {
        keyWord: [],
        hd: '',
        url: '',
        username:'' ,
        title: '' ,
        // 预览框
        dialogVisible: false,
         // 编辑
        dialog: false,
        // 用户页面添加
        dialogadd: false,
        // 搜索添加弹窗
        dialogWares: false,
    },
    mutations: {
        // 商品页搜索框
        updataSearch(state, val) {
            state.keyWord = val;
        },

        hd(state, val) {
            state.hd = val[0]
            state.url = val[1]
        },
        loginUsername(state, val) {
            // console.log(val);
            state.username = val;
        },
        title(state, val) {
            state.title = val
        },
        // 预览框
        dialogVisible(state, val) {
            state.dialogVisible = val
        },
        // 编辑
        dialog(state, val) {
            state.dialog = val
        },
        // 用户页面
        dialogadd(state, val) {
            state.dialogadd = val
        },
        // 搜索添加弹窗
        dialogWares(state, val) {
            state.dialogWares = val
        },
    },
    actions: {
        username($store, data) {
            axios.post('/login', data)
                .then(({ data }) => {
                    if(!data.errno) {
                        $store.commit('loginUsername', data.username)
                        Message({
                            message: data.msg,
                            type: 'success'
                        })
                    }else{
                        Message({
                            message: '请检查用户名和密码',
                            type: 'error'
                        })
                    }
                })
        },
        userinfo($store) {
            axios.get('/userinfo')
                .then(({ data }) => {
                    if(!data.errno) {
                        $store.commit('loginUsername', data.username)
                    }
                })
        }
    }
})